package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.admin.service.NewsAuthService;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.feign.wemedia.wm.WmFeignClients;
import com.heima.model.admin.dtos.NewsAuthRequestDTO;
import com.heima.model.admin.dtos.NewsAuthResponseDTO;
import com.heima.model.user.dtos.AuthDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Fuqiang
 * @since :2023-04-07
 */
@Slf4j
@Service
public class NewsAuthServiceImpl implements NewsAuthService {

    @Autowired
    WmFeignClients wmFeignClient;

    /**
     * 查询自媒体文章人工审核列表
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult<NewsAuthResponseDTO> getListByPageWithTitle(NewsAuthRequestDTO dto) {
        // 调用wemedia服务分页查询自媒体文章信息
        Page<NewsAuthResponseDTO> newsAuthResponseDTOList = wmFeignClient.findByPageWithFeign(dto);
        if (newsAuthResponseDTOList == null) {
            // 数据不存在
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return new PageResult<>(dto, newsAuthResponseDTOList.getTotal(), newsAuthResponseDTOList.getRecords());
    }

    /**
     * 根据自媒体文章id查询自媒体文章详情
     *
     * @param wmNewsId
     * @return
     */
    @Override
    public ResponseResult<NewsAuthResponseDTO> findByWmNewsId(Integer wmNewsId) {
        // feign查询自媒体文章人工审核详情信息
        NewsAuthResponseDTO dto = wmFeignClient.findNewsAuthResponseDTOByWmNewsId(wmNewsId);
        if (dto == null) {
            log.error("无法根据WmNewsId查询到文章详情数据! wmNewsId : {}", wmNewsId);
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.ok(dto);
    }

    @Override
    public void authFail(AuthDTO dto) {
        wmFeignClient.authFail(dto);
    }

    @Override
    public void authPass(AuthDTO dto) {
        wmFeignClient.authPass(dto);
    }
}
